package com.testes.umov.screens;

import java.util.Hashtable;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

import com.testes.umov.constants.Constants;
import com.testes.umov.model.RgbColor;
import com.testes.umov.util.BatteryHelper;
import com.testes.umov.util.BatteryHelperListener;
import com.testes.umov.util.ControllerListener;
import com.testes.umov.view.TemplateActivity;

public class ScreensTestsController implements BatteryHelperListener {

	private Activity context;
	private BatteryHelper batteryHelper;
	private boolean keepGoing = true;
	private List<RgbColor> colorList;
	private int screenCount = 0;
	private ControllerListener listener;
	private int iconColor = 1;
	private PowerManager pm;
	private PowerManager.WakeLock wl;
	
	
	public ScreensTestsController(Activity context, ControllerListener listener) {
		this.context = context;		
		this.listener = listener;
		this.batteryHelper = new BatteryHelper(this.context, this);
	}
	
	public void doTestsWithColors(List<RgbColor> colorList, int iconColor) {
		this.colorList = colorList;
		this.iconColor = iconColor;
		batteryHelper.iniciarMedicao();
		pm = (PowerManager)this.context.getSystemService(Context.POWER_SERVICE);
		wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
		wl.acquire();
		showActivities();
	}
	
	private void showActivities() {
		new Thread(new Runnable() 
		{			
			@Override
			public void run() 
			{
				while(keepGoing) 
				{
					try {
						Thread.sleep(3200);
						context.runOnUiThread(new Runnable() 
						{
							@Override
							public void run() 
							{						
								startAndShowActivity();					
							}
						});
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}				
				}
				
				context.runOnUiThread(new Runnable() 
				{	
					@Override
					public void run() 
					{
						onTestFinished();
					}
				});
			}
		}).start();
	}
	
	private void startAndShowActivity() {
		Intent intent = new Intent(this.context, TemplateActivity.class);
		
		for(RgbColor color : this.colorList) {
			intent.putExtra(color.getKey() + "Red", color.getRed());
			intent.putExtra(color.getKey() + "Green", color.getGreen());
			intent.putExtra(color.getKey() + "Blue", color.getBlue());
		}
		intent.putExtra("iconColor", this.iconColor);
		
		
		this.screenCount++;
		this.context.startActivity(intent);
	}		
	
	@Override
	public void onBatteryLevelChanged(float level, float startingLevel,	float spentLevel) {
		if(spentLevel >= 2) 
		{
			this.keepGoing = false;
		}
	}

	
	public void onTestFinished() 
	{
		wl.release();
		Double spentLevelPerScreen = (Double)2.0 / Double.parseDouble(String.valueOf(screenCount));
		Hashtable<String, Object> results = new Hashtable<String, Object>();
		results.put(Constants.BATTERY_SPENT, spentLevelPerScreen);
		Integer count = (Integer)screenCount;
		results.put(Constants.SCREEN_COUNT, count);
		listener.onTestsFinished(results);			
	}
}
